package com.ysg.utils;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ysg.R;
import com.ysg.widget.recyclerview.divider.HorizontalDividerItemDecoration;

public class YRecyclerViewUtil {

    public static void init(Context context, RecyclerView recyclerView, BaseQuickAdapter adapter){
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context)
                .colorResId(R.color.lineColor)
                .sizeResId(R.dimen.dividerHeight)
//				.marginResId(R.dimen.dp_62,R.dimen.dp_0)//线左右边距
                .build());
        recyclerView.setAdapter(adapter);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    /**
     * 没有分割线
     */
    public static void initNoLine(Context context, RecyclerView recyclerView, BaseQuickAdapter adapter){
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    /**
     * 没有分割线
     */
    public static void initHorizontalNoLine(Context context, RecyclerView recyclerView, BaseQuickAdapter adapter){
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    public static void initGrid(Context context, RecyclerView recyclerView, BaseQuickAdapter adapter, int count){
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(context, count));
        recyclerView.setAdapter(adapter);
    }

    public static void initStagger(Context context, RecyclerView recyclerView, BaseQuickAdapter adapter, int count){
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
    }

    /**
     * 滑动到顶部
     */
    public static void scrollToTop(RecyclerView recyclerView){
        recyclerView.smoothScrollToPosition(0);
    }
    /**
     * 滑动到底部
     */
    public static void scrollToBottom(RecyclerView recyclerView){
        recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount()-1);
    }

    /**
     * 滑动到指定的position:针对线性布局管理器
     * 1.当指定位置位于可视位置之间时：得到距离顶部的距离，然后smoothScrollBy向上滚动固定的距离
     * 2.其他条件：利用利用smoothScrollToPosition实现实现
     *
     * 注意点：scrollToPosition或smoothScrollToPosition，有三种情况
     * 如果：position<firstPosition;则position在第一个item显示
     * 如果：position>lastPosition;则position在最后一个item显示
     * 如果: position>=firstPosition && position<=lastPosition,则滚动不了
     */
    public static void scrollToPosition(RecyclerView recyclerView, int position) {
        //1.获取可见item的第一，最后的下标
        int firstItem;
        int lastItem;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
            firstItem = linearLayoutManager.findFirstVisibleItemPosition();
            lastItem = linearLayoutManager.findLastVisibleItemPosition();
//            linearLayoutManager.scrollToPositionWithOffset(position, 0);
        }else{
            firstItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
            lastItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));
        }

        int top;
        if (position < firstItem || position > lastItem) {
           recyclerView.scrollToPosition(position);
            recyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrollToPosition(recyclerView, position);
                }
            }, 100);

        }else {
            int movePosition = position - firstItem;
            top = recyclerView.getChildAt(movePosition).getTop();
            recyclerView.smoothScrollBy(0, top);
        }

    }
}
